﻿using NLog;
using System.Reflection;

namespace Topro.Log
{
    /// <summary>
    /// 请求日志
    /// </summary>
    public static class HttpLogImpl
    {
        /// <summary>
        /// NLog日志对象
        /// </summary>
        private static readonly Logger httpLog = LogManager.GetLogger("HttpLog");

        /// <summary>
        /// 请求日志
        /// </summary>
        /// <param name="log">日志对象</param>
        public static void Add(HttpLog log)
        {
            var logEventInfo = new LogEventInfo(LogLevel.Info, "HttpLog", "Http Log.");
            //请求
            logEventInfo.Properties["ApplicationName"] = Assembly.GetEntryAssembly()?.GetName().Name;
            logEventInfo.Properties["RequestIp"] = log.RequestIp;
            logEventInfo.Properties["RequestUri"] = log.RequestUri;
            logEventInfo.Properties["RequestQueryString"] = log.RequestQueryString;
            logEventInfo.Properties["RequestContentType"] = log.RequestContentType;
            logEventInfo.Properties["RequestMethod"] = log.RequestMethod;
            logEventInfo.Properties["RequestBody"] = log.RequestBody;
            logEventInfo.Properties["RequestCookies"] = log.RequestCookies;
            logEventInfo.Properties["RequestHeaders"] = log.RequestHeaders;
            logEventInfo.Properties["RequestTime"] = log.RequestTime.ToString("yyyy-MM-dd HH:mm:ss.ffffff");
            //响应
            logEventInfo.Properties["ResponseBody"] = log.ResponseBody;
            logEventInfo.Properties["ResponseStatusCode"] = log.ResponseStatusCode;
            logEventInfo.Properties["ResponseContentType"] = log.ResponseContentType;
            logEventInfo.Properties["ResponseHeaders"] = log.ResponseHeaders;
            logEventInfo.Properties["ResponseTime"] = log.ResponseTime.ToString("yyyy-MM-dd HH:mm:ss.ffffff");

            httpLog.Log(logEventInfo);
        }
    }
}
